//
//  NewsDetailModels.swift
//  网易新闻
//
//  Created by wl on 15/11/22.
//  Copyright © 2015年 wl. All rights reserved.
//  一些新闻详情的数据模型
/***************************************************
*  如果您发现任何BUG,或者有更好的建议或者意见，请您的指教。
*邮箱:wxl19950606@163.com.感谢您的支持
***************************************************/
//========================================================
// 加入数据存储后，代码变得更加恶心，如果你有更好的办法一定要联系我
// 感激不尽！
//========================================================
import Foundation
import SwiftyJSON

//========================================================
// MARK: 新闻详情所用模型
//========================================================
/**
*  新闻详情页面中，某一张图片的模型
*/
class NewsDetailImgModel: NSObject, NSCoding, NewsModelInitProtocol{
    /// 图片的位置
    var ref: String
    /// 图片大小
    var pixel: String
    /// 图片的描述信息
    var alt: String
    /// 图片位置
    var src: String

    required init(json: JSON) {
        ref = json["ref"].stringValue
        pixel = json["pixel"].stringValue
        alt = json["alt"].stringValue
        src = json["src"].stringValue
    }
    
    required init?(coder decoder: NSCoder) {
        ref = decoder.decodeObjectForKey("ref") as! String
        pixel = decoder.decodeObjectForKey("pixel") as! String
        alt = decoder.decodeObjectForKey("alt") as! String
        src = decoder.decodeObjectForKey("src") as! String
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(ref, forKey: "ref")
        aCoder.encodeObject(pixel, forKey: "pixel")
        aCoder.encodeObject(alt, forKey: "alt")
        aCoder.encodeObject(src, forKey: "src")
    }
}
/**
 *  新闻详情里的热门评论里的子评论
 */
class NewsDetailHotReply:NewsModelInitProtocol{
    var id: String
    var commentid: String
    var message: String
    var userAddress: String
    var isdianzan: Bool
    var replycomment: String
    var reply: String
    var childnum: String
    var suppose: String
    var infocomments: [NewsDetailHotReply]
    var floor: String
    var timg: String
    var user: String
    var name: String
    var commenttime: String
    required init(json: JSON) {
        id = json["id"].stringValue
        commentid = json["commentid"].stringValue
        message = json["message"].stringValue
        userAddress = json["userAddress"].stringValue
        isdianzan = json["isdianzan"].boolValue
        replycomment = json["replycomment"].stringValue
        reply = json["reply"].stringValue
        childnum = json["childnum"].stringValue
        suppose = json["suppose"].stringValue
        floor = json["floor"].stringValue
        timg = json["timg"].stringValue
        user = json["user"].stringValue
        name = json["name"].stringValue
        commenttime = json["commenttime"].stringValue
        infocomments = ModelArrayProvider.arrayModel(NewsDetailHotReply.self, json: json["infocomments"])!
    }

}
/**
 *  新闻详情里的热门评论
 */
class NewsDetailHotReplyModel: NewsModelInitProtocol{
    var id: String
    var commentid: String
    var message: String
    var userAddress: String
    var isdianzan: Bool
    var replycomment: String
    var reply: String
    var childnum: String
    var suppose: String
    var infocomments: [NewsDetailHotReply]?
    var floor: String
    var timg: String
    var user: String
    var name: String
    var commenttime: String
    var isme: Bool
        required init(json: JSON) {
            id = json["id"].stringValue
            commentid = json["commentid"].stringValue
            message = json["message"].stringValue
            userAddress = json["userAddress"].stringValue
            isdianzan = json["isdianzan"].boolValue
            replycomment = json["replycomment"].stringValue
            reply = json["reply"].stringValue
            childnum = json["childnum"].stringValue
            suppose = json["suppose"].stringValue
            floor = json["floor"].stringValue
            timg = json["timg"].stringValue
            user = json["user"].stringValue
            name = json["name"].stringValue
            commenttime = json["commenttime"].stringValue
            infocomments = ModelArrayProvider.arrayModel(NewsDetailHotReply.self, json: json["infocomments"])
            isme = json["isme"].boolValue
            
    }
    

}
/**
*  新闻详情模型
*/
class NewsDetailModel: NewsModelInitProtocol {
    /// 新闻标题
    var title: String
    /// 新闻发布时间
    var ptime: String!
    /// 新闻内容
    var body: String
    /// 新闻详情的图片模型
    var img: [NewsDetailImgModel]
    /// 跟帖请求要用到的
    var replyBoard: String
    /// 新闻id请求要用到的
    var docid: String
    
    var isCollection: Bool
    //视频
    var videosrc: String
    
    //供稿人
    var contributor: String!
    
    var url3w: String?
    
    var rmlist: [NewsDetailHotReplyModel]?
    var tags: String!
    var isshoucang: Bool
    var timg:String
    
    var taobaourl: String
    
    var adsimgsrc: String
    
    var adsurl: String
    
    var userid: String
    
    var type: String
    
    required init(json: JSON) {
        title = json["title"].stringValue
        ptime = json["ptime"].stringValue
        body = json["body"].stringValue
        img = ModelArrayProvider.arrayModel(NewsDetailImgModel.self, json: json["img"])!
        replyBoard = json["replyBoard"].stringValue
        docid = json["docid"].stringValue
        isCollection = json["isshoucang"].boolValue
        videosrc = json["videosrc"].stringValue
        contributor = json["contributor"].stringValue
        rmlist = ModelArrayProvider.arrayModel(NewsDetailHotReplyModel.self, json: json["rmlist"])
        url3w = json["url3w"].stringValue
        isshoucang = json["isshoucang"].boolValue
        tags = json["tags"].stringValue
        timg = json["timg"].stringValue
        taobaourl = json["taobaourl"].stringValue
        adsimgsrc = json["adsimgsrc"].stringValue
        adsurl = json["adsurl"].stringValue
        userid = json["userid"].stringValue
        type = json["type"].stringValue
        
    }
    

    
}
//========================================================
// MARK: 专题新闻所用模型
//========================================================
/**
*  专题内的子话题模型
*/
class Topic: NSObject, NSCoding, NewsModelInitProtocol{
    /// 话题名
    var tname: String
    /// 索引
    var index: Int
    /// 话题内的新闻
    var docs: [NewsModel]
    
    required init(json: JSON) {
        tname = json["tname"].stringValue
        index = json["index"].intValue
        docs = ModelArrayProvider.arrayModel(NewsModel.self, json: json["docs"])!
    }
    
    required init?(coder decoder: NSCoder) {
        tname = decoder.decodeObjectForKey("tname") as! String
        index = decoder.decodeObjectForKey("index") as! Int
        docs = decoder.decodeObjectForKey("docs") as! [NewsModel]
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(tname, forKey: "tname")
        aCoder.encodeObject(index, forKey: "index")
        aCoder.encodeObject(docs, forKey: "docs")
    }
}
/**
 *  专题新闻子模型
 */
class Infomationlist: NSObject, NSCoding, NewsModelInitProtocol{
  
    var id: String
    var menuurl: String
    var tname: String
    var title: String
    var ptime: String
    var imgsrc: String
    var digest: String
    var replyCount: String
    var tags: String
    var docid: String
    var body: String
    var img: [specialImg]
    var isshoucang: Bool
    var browsenum: String
    var sharenum: String
    var videosrc: String
    var contributor: String
    var parentid: String
    var type: String
    
    required init(json: JSON) {
        id = json["id"].stringValue
        menuurl = json["menuurl"].stringValue
        tname = json["tname"].stringValue
        title = json["title"].stringValue
        ptime = json["ptime"].stringValue
        imgsrc = json["imgsrc"].stringValue
        digest = json["digest"].stringValue
        replyCount = json["replyCount"].stringValue
        tags = json["tags"].stringValue
        docid = json["docid"].stringValue
        body = json["body"].stringValue
        img = ModelArrayProvider.arrayModel(specialImg.self, json: json["img"])!
        isshoucang = json["isshoucang"].boolValue
        browsenum = json["browsenum"].stringValue
        sharenum = json["sharenum"].stringValue
        videosrc = json["videosrc"].stringValue
        contributor = json["contributor"].stringValue
        parentid = json["parentid"].stringValue
        type = json["type"].stringValue

    }
    
    required init?(coder decoder: NSCoder) {
         id = decoder.decodeObjectForKey("id") as! String
         menuurl = decoder.decodeObjectForKey("menuurl") as! String
         tname = decoder.decodeObjectForKey("tname") as! String
         title = decoder.decodeObjectForKey("title") as! String
         ptime = decoder.decodeObjectForKey("ptime") as! String
         imgsrc = decoder.decodeObjectForKey("imgsrc") as! String
         digest = decoder.decodeObjectForKey("digest") as! String
         replyCount = decoder.decodeObjectForKey("replyCount") as! String
         tags = decoder.decodeObjectForKey("tags") as! String
         docid = decoder.decodeObjectForKey("docid") as! String
         body = decoder.decodeObjectForKey("body") as! String
         img = decoder.decodeObjectForKey("img") as! [specialImg]
         isshoucang = decoder.decodeObjectForKey("isshoucang") as! Bool;
         browsenum = decoder.decodeObjectForKey("browsenum") as! String
         sharenum = decoder.decodeObjectForKey("sharenum") as! String
         videosrc = decoder.decodeObjectForKey("videosrc") as! String
         contributor = decoder.decodeObjectForKey("contributor") as! String
         parentid = decoder.decodeObjectForKey("parentid") as! String
         type = decoder.decodeObjectForKey("type") as! String

        
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(menuurl, forKey: "menuurl")
        aCoder.encodeObject(tname, forKey: "tname")
        aCoder.encodeObject(title, forKey: "title")
        aCoder.encodeObject(ptime, forKey: "ptime")
        aCoder.encodeObject(imgsrc, forKey: "imgsrc")
        aCoder.encodeObject(digest, forKey: "digest")
        aCoder.encodeObject(replyCount, forKey: "replyCount")
        aCoder.encodeObject(tags, forKey: "tags")
        aCoder.encodeObject(docid, forKey: "docid")
        aCoder.encodeObject(body, forKey: "body")
        aCoder.encodeObject(img, forKey: "img")
        aCoder.encodeObject(isshoucang, forKey: "isshoucang")
        aCoder.encodeObject(browsenum, forKey: "browsenum")
        aCoder.encodeObject(sharenum, forKey: "sharenum")
        aCoder.encodeObject(videosrc, forKey: "videosrc")
        aCoder.encodeObject(contributor, forKey: "contributor")
        aCoder.encodeObject(parentid, forKey: "parentid")
        aCoder.encodeObject(type, forKey: "type")
        
    }
}
class specialImg: NSObject, NSCoding, NewsModelInitProtocol{
    
    var id: String
    var ref: String
    var pixel: String
    var alt: String
    var src: String

    required init(json: JSON) {
        id = json["id"].stringValue
        ref = json["ref"].stringValue
        pixel = json["pixel"].stringValue
        alt = json["alt"].stringValue
        src = json["src"].stringValue
        
        
    }
    
    required init?(coder decoder: NSCoder) {
        id = decoder.decodeObjectForKey("id") as! String
        ref = decoder.decodeObjectForKey("ref") as! String
        pixel = decoder.decodeObjectForKey("pixel") as! String
        alt = decoder.decodeObjectForKey("alt") as! String
        src = decoder.decodeObjectForKey("src") as! String
   
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(ref, forKey: "ref")
        aCoder.encodeObject(pixel, forKey: "pixel")
        aCoder.encodeObject(alt, forKey: "alt")
        aCoder.encodeObject(src, forKey: "src")
        
    }
}
/**
*  专题新闻模型
*/

class NewsSpecialModel: NSObject, NSCoding {

    var id: String
    var digest: String
    var infomationlist: [NewsModel]

    init(json: JSON) {
        id = json["id"].stringValue
        digest = json["digest"].stringValue
        infomationlist = (ModelArrayProvider.arrayModel(NewsModel.self, json: json["infomationlist"]))!
    }
    
    required init?(coder decoder: NSCoder) {

        id = decoder.decodeObjectForKey("id") as! String
        digest = decoder.decodeObjectForKey("digest") as! String
        infomationlist = decoder.decodeObjectForKey("infomationlist") as! [NewsModel]
        
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(id, forKey: "id")
        aCoder.encodeObject(digest, forKey: "digest")
        aCoder.encodeObject(infomationlist, forKey: "infomationlist")
    }
}

//========================================================
// MARK: 图片新闻所用模型
//========================================================
/**
*  图片新闻的图片模型
*/
class Photo: NSObject, NSCoding, NewsModelInitProtocol{
    /// 描述
    var note: String
    var imgurl: String
    
    required init(json: JSON) {
        note = json["noteString"].stringValue
        imgurl = json["imgUrl"].stringValue
    }
    
    required init?(coder decoder: NSCoder) {
        note = decoder.decodeObjectForKey("noteString") as! String
        imgurl = decoder.decodeObjectForKey("imgUrl") as! String
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(note, forKey: "noteString")
        aCoder.encodeObject(imgurl, forKey: "imgUrl")
    }
}
/**
*  图片新闻的模型
*/
class NewsPictureModel: NSObject, NSCoding {
    /// 图片新闻内容
    var photos: [Photo]
    /// 名称
    var setname: String
    /// 数量
    var imgsum: Int
    /// 跟帖请求要用到的
    var boardid: String
    /// 新闻id请求要用到的
    var postid: String
    
    init(json: JSON) {
        setname = json["setname"].stringValue
        imgsum = json["imgsum"].intValue
        photos = ModelArrayProvider.arrayModel(Photo.self, json: json["photos"])!
        boardid = json["boardid"].stringValue
        postid = json["postid"].stringValue
    }
    
    required init?(coder decoder: NSCoder) {
        setname = decoder.decodeObjectForKey("setname") as! String
        imgsum = decoder.decodeObjectForKey("imgsum") as! Int
        photos = decoder.decodeObjectForKey("photos") as! [Photo]
        boardid = decoder.decodeObjectForKey("boardid") as! String
        postid = decoder.decodeObjectForKey("postid") as! String
    }
    
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(setname, forKey: "setname")
        aCoder.encodeObject(imgsum, forKey: "imgsum")
        aCoder.encodeObject(photos, forKey: "photos")
        aCoder.encodeObject(boardid, forKey: "boardid")
        aCoder.encodeObject(postid, forKey: "postid")
    }
    
}

//========================================================
// MARK: 新闻评论所用模型
//========================================================
/**
 * 一条评论内容的模型
 */
class ReplyModel {
    var id: Int
 /// 用户名
    var name: String
 /// 用户的地址信息
    var userAddress: String
 /// 用户发言
    var message: String
 /// 赞，楼主才有
    var suppose: String?
    /// 头像
    var timg: String?
 /// 这条评论的楼数
    var floor: Int
    
    var commentid: Int
    
    var childnum: Int
    
    var infocomments: [ReplyModel] = []
    
    var isdianzan: Bool
    
    var userID: Int
    
    init(json: JSON) {
        id = json["id"].intValue
        floor = json["floor"].intValue
        name = json["name"].string ?? "火星网友"
        userAddress = json["userAddress"].stringValue
        message = json["message"].stringValue
        suppose = json["suppose"].string
        timg = json["timg"].string
        commentid = json["commentid"].intValue
        childnum = json["childnum"].intValue
        let info = json["infocomments"].array
        if info != nil {
            for item in info! {
                infocomments.append(ReplyModel.init(json: item))
            }
        }
        isdianzan = json["isdianzan"].boolValue
        userID = json["user"].intValue
    }
}







// 版权属于原作者
// http://code4app.com (cn) http://code4app.net (en)
// 发布代码于最专业的源码分享网站: Code4App.com